package comparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : ListDemo
 * @Author : 54061
 * @Date: 2022/2/9 18:29
 */
public class ListDemo  {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(1);
        list.add(3);
        list.add(2);
        list.add(4);
        list.add(5);
        list.add(1);
        list.add(3);
        list.add(2);
        list.add(4);
        System.out.println("list = " + list);

        //对List集合进行排序
        Collections.sort(list);
        System.out.println("list = " + list);

        ArrayList<Student> list1 = new ArrayList<>();
        list1.add(new Student("张三",17));
        list1.add(new Student("李四",15));
        System.out.println("list1 = " + list1);
        //解决方案1: 提供绑定比较器
        //解决方案2: 提供独立比较器
        Collections.sort(list1, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.age - o2.age;
            }
        });
        System.out.println("list1 = " + list1);
    }
}
